<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!-- 角色权限页面布局 -->
<div id="priv-role-layout" class="easyui-layout" style="width:100%;height:100%;">   
	<div data-options="region:'west',title:'角色列表',split:false" style="width:200px">
		<%--角色列表--%>
		<table id="priv-role-datagrid"></table>
	</div>   
	<div data-options="region:'center',title:'权限设置'" style="padding:5px;background:#eee;">
		<!-- 具体角色的权限设置布局 -->
		<div id="priv-role-layout-layout" class="easyui-layout" style="width:100%;height:100%;">   
		    <div data-options="region:'north',title:'角色信息'" style="height:100px;">
		    	<h1>当前角色：<span id="role-name">xxx</span></h1>
		    </div>   
		    <div data-options="region:'center',title:'设置权限'" style="padding:5px;background:#eee;">
		    	<!-- 选项卡 -->
		    	<div id="priv-role-tabs" class="easyui-tabs" style="width:100%;height:100%;">   
				    <div title="菜单权限" style="padding:20px;display:none;">   
				        <ul id="priv-role-menu-tree" data-options="checkbox:true,method:'get',cascadeCheck:false"></ul>
				        <!-- 保存按钮 -->
				        <div style="margin: 30px"></div>
				        <a id="priv-role-menu-linkbutton" style="display: none;" onclick="javascript:priv_role.saveMenuPrivs()"
				        href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">保  存</a>  
				    </div>   
				    <div title="功能权限" style="overflow:auto;padding:20px;display:none;">   
				        <ul id="priv-role-func-tree" data-options="checkbox:true,method:'get',cascadeCheck:false,onlyLeafCheck:true"></ul>
				        <div style="margin: 30px"></div>
				         <!-- 保存按钮 -->
				        <a id="priv-role-func-linkbutton" style="display: none;" onclick="javascript:priv_role.saveFuncPrivs()"
				        href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">保  存</a>     
				    </div>   
				</div>  
		    </div>   
		</div>  
	</div>   
</div> 

<script type="text/javascript">
//定义一个包名
var priv_role = {roleId:0};

//定义页面初始化方法
priv_role.init = function(){
	$('#priv-role-datagrid').datagrid({    
	    url:'/priv/role/list', 
	    method:'get',
	    columns:[[    
	        {field:'id',title:'id',hidden:true},    
	        {field:'name',title:'角色名称',width:198,align:'center'},     
	    ]],
	    singleSelect:true,
	    pagination:true,
	    pageSize:20
	});

	//设置单击事件
	$('#priv-role-datagrid').datagrid({
		onClickRow: function(rowIndex, rowData){
			//获取角色id
			roleId = rowData['id'];
			//设置角色信息
			var role_name = rowData['name'];
			$('#priv-role-layout #role-name').text(role_name);
			//设置选项卡的信息
			var title = $('#priv-role-tabs').tabs('getSelected').panel('options').title;
			if(title=='菜单权限'){
				priv_role.loadMenu(roleId);
				//显示按钮
				$("#priv-role-menu-linkbutton").show();
			}else if(title=='功能权限'){
				priv_role.loadFunc(roleId);
				$("#priv-role-func-linkbutton").show();
			}
		}
	});

	//修改分页属性
	var pager = $('#priv-role-datagrid').datagrid('getPager');
	$(pager).pagination({ 
		layout:['prev','next']
	});
	//获取分页div
	$('#priv-role-layout  div.datagrid-pager.pagination').css("width","198");
	
	//初始化选项卡
	$('#priv-role-tabs').tabs({  
	    onSelect:function(title){    
	        //根据选中的角色，加载对应的权限数据
	    	var row = $('#priv-role-datagrid').datagrid('getSelected');
	        if(row==null){
	        	return;
	        }else{
	        	if(title=='菜单权限'){
					priv_role.loadMenu(row.id);
					$("#priv-role-menu-linkbutton").show();
				}else if(title=='功能权限'){
					priv_role.loadFunc(row.id);
					$("#priv-role-func-linkbutton").show();
				}
	        }
	    }    
	});
}

//加载菜单数据方法
priv_role.loadMenu=function(roleId){
	$('#priv-role-menu-tree').tree({    
	    url:'/priv/role/menu/tree',
	    queryParams:{'roleId':roleId}
	});  
};

//加载功能数据方法
priv_role.loadFunc=function(roleId){
	$('#priv-role-func-tree').tree({    
	    url:'/priv/role/func/tree',
	    queryParams:{'roleId':roleId}
	}); 
};
//定义保存菜单权限方法
priv_role.saveMenuPrivs=function(){
	//获取当前选择的菜单
	var nodes = $('#priv-role-menu-tree').tree('getChecked');
	//获取菜单ids
	var ids = '';
	for(var index in nodes){
		var id = nodes[index].id;
		ids += id +",";
	}
	//利用ajax向服务器提交数据
	$.ajax({
	   type: "POST",
	   url: "/priv/role/menu/add",
	   data: "roleId="+roleId+"&ids="+ids,
	   dataType: "json",
	   success: function(msg){
	     if(msg.status==200){
	    	 $.messager.alert('提示消息','保存菜单权限成功！','info');
	     }
	   }
	});
}
//定义保存功能权限方法
priv_role.saveFuncPrivs=function(){
	//获取当前选择的功能
	var nodes = $('#priv-role-func-tree').tree('getChecked');
	//获取功能ids
	var ids = '';
	for(var index in nodes){
		var id = nodes[index].id;
		ids += id +",";
	}
	//利用ajax向服务器提交数据
	$.ajax({
	   type: "POST",
	   url: "/priv/role/func/add",
	   data: "roleId="+roleId+"&ids="+ids,
	   dataType: "json",
	   success: function(msg){
	     if(msg.status==200){
	    	 $.messager.alert('提示消息','保存功能权限成功！','info');
	     }
	   }
	});
}
//页面加载完毕，执行初始化
priv_role.init();
</script> 